<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include "database.php";
include "global.php";


function query_type($product_id){
    $sql = "SELECT name,ratio,asset,nameunit,id FROM product_type  WHERE product_id=".$product_id;
   // echo $sql;
    $product = mysql_query($sql);

    return json_encode($product);
}
function query_product($product_id){
    $sql = "SELECT id,name FROM product  WHERE product_id=".$product_id;
   // echo $sql;
    $product = mysql_query($sql);
    $product = mysql_fetch_array($product);
   // echo json_encode($product);
    $output['product']['id'] =$product['id'];
    $output['product']['name'] =$product['name'];
    $sql = "SELECT name,id,nameunit,ratio,buyprice FROM product_type  WHERE product_id=".$product['id'];
   // echo $sql;
    $query = mysql_query($sql);
    
    $i=0;
    while ($row = mysql_fetch_array($query)) {
        $output['type'][$i]['name']=$row['name'];
        $output['type'][$i]['id']=$row['id'];
        $output['type'][$i]['nameunit']=$row['nameunit'];
        $output['type'][$i]['ratio']=$row['ratio'];
        $output['type'][$i]['buyprice']=$row['buyprice'];
        
        $i++;
    }
    
echo json_encode($output);
    
}


function addStock($getItem){
    extract($getItem);
    $newadd = $ratio * $addStock;

    $sql="UPDATE product SET amount=amount+".$newadd." WHERE id=".$id;
   echo $sql;
    if(!mysql_query($sql))
        echo "Update Stock error";
    $sql="INSERT INTO stock_log(product_id,product_type_id,amount) VALUES(".$id.",".$sort.",".$addStock.")";
    if(!mysql_query($sql))
        echo "Add Stock_Log error";
}
function showAll(){
    $sql = "SELECT S.id,P.product_id,T.name,T.ratio,S.amount,T.nameunit FROM product_type T,product P,stock_log S WHERE S.product_type_id=T.id AND P.id=S.product_id ORDER BY S.product_id";
    //echo $sql;
    $output = mysql_query($sql);

    return $output;
}
function delete($get){
    extract($get);
    $sumdel = $ratio * $amount;

    $sql="UPDATE product SET amount=amount-".$sumdel." WHERE product_id='".$product."'";
   echo $sql;
    if(!mysql_query($sql))
        echo "Update Product error";
    $sql="DELETE FROM stock_log WHERE id=".$id;
    echo $sql;
    if(!mysql_query($sql))
        echo "Delete Stock error";

}

?>
